﻿using static CyberU;
using static CyberU.UI;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.Serialization;

public class ConnectLine : MonoBehaviour
{
    public GameObject start;
    public GameObject end;
    public LineRenderer lineRenderer;
    public Vector3 middlePosition;

    public void Initial(GameObject start, GameObject end)
    {
        // Delog("set start and end",start, end);
        this.start = start;
        this.end = end;
        lineRenderer = AddOrGetComponent<LineRenderer>(gameObject);
        lineRenderer.positionCount = 2;
        UpdateLine();
    }
    
    private void Update()
    {
        OnUpdate();
    }
    public void OnUpdate()
    {
        UpdateLine();
        middlePosition=(start.transform.position+end.transform.position)/2f;
        angle = Mathf.Atan2(end.transform.position.y - start.transform.position.y, end.transform.position.x - start.transform.position.x) * Mathf.Rad2Deg;
    }

    public float angle;
    private void UpdateLine()
    {
        if (start.transform != null && end.transform != null)
        {
            lineRenderer.SetPosition(0, start.transform.position);
            lineRenderer.SetPosition(1, end.transform.position);
            return;
        }
        // lineRenderer.SetPosition(0, start.transform.position);
        // lineRenderer.SetPosition(1, end.transform.position);
    }
}